package com.fit.pages.StudentOfTheCourse;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.internal.antlr.PropertyExpressionParser.list_return;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tapestry5.ioc.services.TypeCoercer;
import org.apache.tapestry5.util.EnumSelectModel;
import org.apache.tapestry5.util.EnumValueEncoder;
import org.apache.tools.ant.taskdefs.Sleep;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.AnonymousAccess;
import com.fit.annotations.ProfessorAccess;
import com.fit.annotations.StudentAccess;
import com.fit.dao.studentOfTheCourse.StudentOfTheCourseDAO;
import com.fit.dao.course.CourseDAO;
import com.fit.dao.language.LanguageDAO;
import com.fit.dao.user.UserDAO;
import com.fit.entities.studentOfTheCourse.StudentOfTheCourse;
import com.fit.entities.course.Course;
import com.fit.entities.language.Language;
import com.fit.entities.user.User;
import com.fit.lov.generic.GenericSelectModel;
import com.fit.lov.user.UserSelectModel;
import com.fit.model.UserRole;
import com.fit.pages.ErrorPage;
import com.fit.services.Authenticator;

@AdminAccess
@AnonymousAccess
@ProfessorAccess
@StudentAccess
public class Add {

	private GenericSelectModel<User> userModel;
	private GenericSelectModel<Course> courseModel;
	private GenericSelectModel<User> studentModel;

	// Inject
	@Inject
	private PropertyAccess access;
	@Inject
	private Session session;
	@Inject
	private Messages messages;
	@Inject
	private Authenticator authenticator;
	@Inject
	private UserDAO userDAO;
	@Inject
	private CourseDAO courseDAO;
	@Property
	private StudentOfTheCourse studentOfTheCourse;	
	@Property
	private User user;
	@Property
	private Course course;
	@Inject
	private StudentOfTheCourseDAO studentOfTheCourseDAO;
	@Property
	@Persist
	private Course selectedCourse;
	@Property
	@Persist
	private List<User> selectedStudents;

	/*PALETA*/
	public GenericSelectModel<User> getStudentModel(){
		if (selectedCourse==null) {
			List<User> u = session.createCriteria(User.class).add(Restrictions.eq("role", UserRole.STUDENT)).list();
			studentModel = new GenericSelectModel<User>(u, User.class, "userName", "id", access);
		}else{
			if (selectedCourse!=null) {
		 String upitv="select s.* from users s where s.user_id not in (select so.id from studentOfTheCourse so where so.course_id="+selectedCourse.getCourseId()+") and s.role=2";
		List<User> u = session.createSQLQuery(upitv).addEntity(User.class).list();
		studentModel = new GenericSelectModel<User>(u, User.class, "userName", "id", access);
			}
		}
		return studentModel;
	}
	/*PALETA*/

	
	
	@Persist
	@Property
	private User selectedUser;

	@Persist
	@Property
	private int grade;

	
	public GenericSelectModel<User> getUserModel(){
		List<User> s = session.createCriteria(User.class).add(Restrictions.sqlRestriction("role = 2")).list();
		userModel = new GenericSelectModel<User>(s, User.class, "userName", "id", access);
		return userModel;
	}
	
	
	public GenericSelectModel<Course> getCourseModel() {
		List<Course> c = courseDAO.findAll();
		courseModel = new GenericSelectModel<Course>(c, Course.class, "language", "courseId", access);
		return courseModel;
	}

	

	Object onSubmitFromAddTestForm()
	{
		System.out.println("The form was submitted!");
		return null;
	}


	@OnEvent(value="selected", component="submitButton")
	void onSubmitButton()
	{
	
		for (int i = 0; i < selectedStudents.size(); i++) {
			StudentOfTheCourse c = new StudentOfTheCourse();
			c.setCourse(selectedCourse);
			c.setGrade(grade);
			c.setUser(selectedStudents.get(i));
			session.save(c);
		}
		selectedStudents = null;

	}
	
	
	
	
	
	
			

	@OnEvent(component = "ResetConfLink")
	Object onResetConfLink() {
		return null;
	}

	@OnEvent(component = "ResetErrorLink")
	Object onResetErrorLink() {
		return null;
	}



}
